/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author heguanghua
 * @since 2018/10/18
 */
var form = null;

layui.define(['form', 'layedit', 'laydate', 'upload'], function (exports) {
    let $ = layui.$;
    form = layui.form;
    let layer = layui.layer;
    let upload = layui.upload;
    let index;

    form.verify({
        minSort: function(value, item){ //value：表单的值、item：表单的DOM对象
            if (value < 1){
                return "排序号不能小于1";
            }
            if (value > 10000000){
                return "排序号不能大于10000000";
            }
        }, templetId: function(value,item){
            if (value !== "") {
                if (!/^[0-9]+$/.test(value)) {
                    return "模板ID只能是正整数";
                }
            }
        }
    });

    zmnLayUtil.layFormVerify(form);

    form.render(null, "zmn-form");

    reLoadImage("coverSrc", json.obj.coverSrc, "coverSrcValue");

    //初始化文件上传插件
    uploaderImage("coverSrc", "coverSrcValue", "png|jpg|jpeg", 1024);

    // 提交
    form.on('submit(zmn-form-submit)', function (obj) {
        let data = obj.field;

        let sendTypeArr = new Array();
        let sendType = $('input[name="sendType"]');
        for(let k in sendType){
            if(sendType[k].checked)
                sendTypeArr.push(sendType[k].value);
        }
        if(sendTypeArr.length == 0){
            layer.msg("发送方式不能为空");
            return false;
        }

        data.sendType = sendTypeArr.join(",");

        if (data.sendType.indexOf("3") != -1) {
            if (data.templateId == null || data.templateId == undefined || data.templateId == '') {
                layer.msg("发送方式包含短信，短信模板ID不能为空");
                return false;
            }
        }

        let url = "/msg/conf/addEdit.action";
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            beforeSend: function () {
                index = layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            },
            success: function (json) {
                layer.close(index);
                layer.msg(json.message);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            },
            complete: function () {
                layer.close(index);
            }
        })

        return false;
    });

    /**
     * 描述: 图片上传方法
     * @param elemId
     * @param valueId
     */
    let indexUploadImags;

    function uploaderImage(elemId, valueId, exts, size) {
        upload.render({
            elem: '#' + elemId
            , exts: exts
            , acceptMime: 'image/*'
            , accept: 'images'
            ,url: '/common/uploadFile.action' //改成您自己的上传接口
            ,data: {folderName: 'msgConf'}
            , size: size
            , before: function (obj) {
                indexUploadImags = layui.layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            }
            , done: function (res) {
                if (res.code == 2) {
                    var data = res.data;
                    $("#" + valueId).val(data.src);
                    $("#" + elemId).attr("style", "background:url( " + data.src + "?" + Date.now() + ");background-size:100% 100%;background-repeat:no-repeat;");
                    let next = $("#" + valueId).next();
                    let next1 = $(next).next();
                    $(next).attr("style", "visibility:hidden !important");
                    $(next1).attr("style", "visibility:hidden !important");
                } else {
                    layer.msg("上传失败！")
                }
                layui.layer.close(indexUploadImags);
            }
        });
    }

    function reLoadImage(elemId, value, valueId) {
        zmnUploadUtil.reLoadImage($, elemId, value, valueId);
    }

    //对外暴露的接口
    exports('msg_conf_detail_edit_js', {});
});

function deleteImg(element) {
    var elementValue = element+'Value';
    document.getElementById(element).removeAttribute('style');
    Array.from(document.getElementById(element).children).forEach(function(item){
        item.removeAttribute('style');
    });
    document.getElementById(elementValue).removeAttribute('value');
}

function previewImg(element) {
    //调整预览大小
    var img = new Image();
    img.src = document.getElementById(element).value;
    // img.src = obj.src;
    var imgHtml = "<img src='" + img.src + "' width='100%' height='100%'/>";
    //弹出层
    layer.open({
        type: 1,
        shade: 0.8,
        offset: 'auto',
        area: [600 + 'px',300+'px'],
        shadeClose:true,
        scrollbar: false,
        title: "", //不显示标题
        content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
        cancel: function () {
            //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', { time: 5000, icon: 6 });
        }
    });
}